#include <string.h>

#include "lib.h"

#include "hand.h"
#include "shue.h"
#include "cardmgr.h"

Hand::Hand(CardMgr* Mgr)
{
	next = (Hand*)0;
	count = 0;
	mgr = Mgr;
}

bool8
Hand::add(uint8 Card)
{
	if (count < 52)
	{
		cards[count++] = Card;
		return true;
	}
	return false;
}

uint8
Hand::getCardAt(int32 index)
{
	if (index >= 0 && index < count)
		return cards[index];
	else
		return (uint8)0xFF;
}

int32
Hand::out(uint8* Buf)
{
	int32 used = 0;
	int32 i;

	for (i = 0; i < count; i++)
		used += outcard(i, Buf + used);

	return used;
}

int32
Hand::outlast(uint8* Buf)
{
	if (count > 0)
		return outcard(count - 1, Buf);

	return 0;
}

int32
Hand::outfirst(uint8* Buf)
{
	if (count > 0)
		return outcard(0, Buf);

	return 0;
}

int32
Hand::outcard(int32 Which, uint8* Buf)
{
	int32 used = 0;

	mgr->encode(cards[Which], Buf, &used);

	return used;
}

void
Hand::restore(uint8* Cards, int32 Count)
{
	count = MIN(Count, 52);

	memcpy(cards, Cards, count);
}
